package sketch.ast;

import java.util.List;

import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.MethodDeclaration;

import sketch.util.SketchUtil;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

public class ASTFormatTest extends TestCase {

	public static Test suite() {
		return new TestSuite(ASTFormatTest.class);
	}
	
	public void testFormatWholeFile() {
		String filePath = "./testfiles/sketch/ast/SampleCodeTests.java";
		CompilationUnit unit = ASTUtils.astRoot(filePath);
		String formatted = ASTFormatter.format(unit);
		SketchUtil.stdln(formatted);
	}
	
	public void testFormatMethod() {
		String filePath = "./testfiles/sketch/ast/SampleCodeTests.java";
		CompilationUnit unit = ASTUtils.astRoot(filePath);
		List<MethodDeclaration> methods = ASTUtils.getAllMethods(unit);
		for(MethodDeclaration method : methods) {
			SketchUtil.stdln("formatting method: " + method.getName().getIdentifier());
			SketchUtil.stdln(ASTFormatter.format(method));
		}
	}
	
	public void testFormatRecoveredMethod() {
		String filePath = "./testfiles/sketch/ast/SampleCodeTests.java";
		CompilationUnit unit = ASTUtils.astRoot(filePath);
		List<MethodDeclaration> methods = ASTUtils.getAllMethods(unit);
		for(MethodDeclaration method : methods) {
			SketchUtil.stdln("formatting recovered method from string: " + method.getName().getIdentifier());
			String methodStr = ASTFormatter.format(method);
			MethodDeclaration[] methodDeclarations = ASTUtils.parseMethods(methodStr);
			assertTrue(methodDeclarations.length == 1);
			SketchUtil.stdln(ASTFormatter.format(methodDeclarations[0]));
		}
	}
}
